import 'package:amoy_ticket/perform/data_info.dart';
import 'package:amoy_ticket/utils/initialization/init_refresh_configuration.dart';
import 'package:amoy_ticket/utils/initialization/init_theme_data.dart';
import 'package:amoy_ticket/utils/router/getRouteConfig.dart';
import 'package:amoy_ticket/widget/splash/splash_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';

import 'utils/initialization/message.dart';
import 'utils/router/router_util.dart';

void main() {
  try {
    // 设置屏幕方法调用之前，需要调用一下 binding 的初始化方法
    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setPreferredOrientations(
            [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
        .then((_) {
      runApp(ChangeNotifierProvider(
        create: (context) => DataInfo(),
        child: MyApp(),
      ));
      RouterUtil.intitRoutes();
      // NetServiceManager().registeredService(CustomService());
    });
  } catch (e) {
    print(e);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Initialization.initRefreshConfiguration(
      child: ScreenUtilInit(
        // 屏幕适配初始化
        designSize: Size(360, 690),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, widget) => ColorFiltered(
          colorFilter: ColorFilter.mode(Colors.transparent, BlendMode.color),
          child: GetMaterialApp(
            title: '影票票',
            enableLog: true,
            theme: InitThemeData().themeData,

            defaultTransition: Transition.fade,

            opaqueRoute: Get.isOpaqueRouteDefault,
            popGesture: Get.isPopGestureEnable,
            translations: Message(),
            locale: Locale('zh', 'CN'), // 中文简体
            fallbackLocale: Locale('en', 'US'), //添加一个回调语言选项，以备上面指定的语言翻译不存在
            localizationsDelegates: [
              PickerLocalizationsDelegate.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: [
              const Locale('zh'),
              const Locale('CN'),
            ],

            debugShowCheckedModeBanner: false,
            home: SplashPage(),
            // initialRoute: RouteConfig.testOne,
            getPages: RouteConfig.getPages,
            navigatorObservers: [FlutterSmartDialog.observer],
            builder: (BuildContext context, Widget? child) {
              ScreenUtil.init(context);
              SmartDialog.config
                ..custom = SmartConfigCustom()
                ..attach = SmartConfigAttach()
                ..loading = SmartConfigLoading()
                ..toast = SmartConfigToast();

              return MediaQuery(
                data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                child: FlutterSmartDialog(child: child),
              );
            },
          ),
        ),
      ),
    );
  }
}
